package raj.scr;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class StudentForm extends JFrame{
	private Connection conn;
	
	private List<Student> studentList = new ArrayList<Student>();
	private List<Course>courseList=new ArrayList<Course>();
	private List<Registration> registrationList =new ArrayList<Registration>();
	
	private GUIPlatform gui;	
	private static int clickCountS;
	private static int clickCountC;
	private static int clickCountR;
	
	
	private JTextField tfID;
	private JTextField tfName;
	private JTextField tfStreet;
	private JTextField tfCity;
	private JTextField tfZip;
	private JTextField tfState;
	
	private JLabel lID;
	private JLabel lName;
	private JLabel lStreet;
	private JLabel lCity;
	private JLabel lZip;
	private JLabel lState;
	
	private JButton bSubmit;
	private JButton bExtension;
	
	public StudentForm(){
		super("Student Course Registration Form");
		gui=new GUIPlatform("Student Course Registration");
		studentList=new ArrayList<Student>();
		this.setContentPane(gui.getContentPane());
		
		//StudentForm
		gui.getUp1().setBackground(Color.blue);
		gui.getLp3().setBackground(new Color(204,0,204));
		this.setTextFields();
		
		//Extension Form
		gui.getLp3().setLayout(new BorderLayout());
		bExtension=new JButton();
		bExtension.setText("Space for Extension");		
		gui.getLp3().add(bExtension,BorderLayout.NORTH);		
		
		
		// Course Form
		
		CourseForm courseForm=new CourseForm();
//		  courseForm.getCoursePanel().setVisible(true); //Acutally this line is not required. 
		  courseForm.getCoursePanel().setBackground(Color.red);
		  courseForm.getCoursePanel().setPreferredSize(new Dimension(170,240));
		gui.getUp3().add(courseForm.getCoursePanel());
	 
		// Registration Form
		RegistrationForm regForm=new RegistrationForm();
		regForm.getRegPanel().setBackground(Color.cyan);
		regForm.getRegPanel().setPreferredSize(new Dimension(170,220));
		gui.getLp1().add(regForm.getRegPanel());
		
		//Submission Form
		SubmissionForm submissionForm=new SubmissionForm();
		submissionForm.getSubmissionPanel().setBackground(new Color(0,240,240));
		submissionForm.getSubmissionPanel().setPreferredSize(new Dimension(170,220));
		gui.getUp2().add(submissionForm.getSubmissionPanel());
		submissionForm.getbStudentRecord().addActionListener(new StudentRecorder());
		submissionForm.getbCourseRecord().addActionListener(new CourseRecorder());
		submissionForm.getbRegisterRecord().addActionListener(new RegistrationRecorder());
		
		//DataManagement Form
		DataManagementForm dataForm=new DataManagementForm();
		 dataForm.getDataPanel().setBackground(new Color(0,30,204));
		 dataForm.getDataPanel().setPreferredSize(new Dimension(170,240));
		  gui.getLp2().add(dataForm.getDataPanel());
		  dataForm.getbStudentRecord().addActionListener(new StudentRetriver());
		  dataForm.getbCourseRecord().addActionListener(new CourseRetriver());
		  dataForm.getbRegistrationRecord().addActionListener(new RegistrationRetriver());
	}// constructor closer
	
	public void setTextFields(){
		gui.getUp1().setLayout(new GridLayout(6,2));
	
		lID=new JLabel("  ID");
		gui.getUp1().add(lID);
		tfID=new JTextField();
	    gui.getUp1().add(tfID);
		
	    lName=new JLabel("  Name");
		gui.getUp1().add(lName);
		tfName=new JTextField();
		gui.getUp1().add(tfName);
		
		lStreet=new JLabel("  Street");
		gui.getUp1().add(lStreet);
		tfCity= new JTextField();
		gui.getUp1().add(tfCity);
		
		lCity=new JLabel("  City");
		gui.getUp1().add(lCity);
		tfStreet=new JTextField();
		gui.getUp1().add(tfStreet);
		
		lZip=new JLabel("  Zip");
		gui.getUp1().add(lZip);
		tfZip=new JTextField();
		gui.getUp1().add(tfZip);
		
		lState=new JLabel("  State");
		gui.getUp1().add(lState);
		tfState=new JTextField();
		gui.getUp1().add(tfState);
	}
	
	
public static void makeGUI(){
	StudentForm studentForm=new StudentForm();
	studentForm.setSize(500,500);
	studentForm.setPreferredSize(new Dimension(500,500));
//	studentForm.setBackground(new Color(204,0,204));
	studentForm.setVisible(true);
	studentForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// makeGUI method closer

public class StudentRecorder implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		readStudentForm();
		System.out.println("value of clickCount=   "+clickCountS);
		writeStudentForm();
		
		if(clickCountS>0)
			System.out.println(studentList.get(clickCountS-1));
			
		}	
	}// class Handler closer
public class StudentRetriver implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		if(studentList!=null){
			studentList.removeAll(studentList);
		}
		if(conn==null){
			try {
				conn=UtilityClass.getConnection();
			} catch (SQLException e1) {
				System.out.println("Connection is not reachable or connectable");
				e1.printStackTrace();
			}
		}// if closer for connection
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery("select * from student");
			//studentList=new ArrayList<Student>();
			while(rs.next()){
		Student s=new Student();
		studentList.add(s);
		s.setStudentID(rs.getInt(1));
		s.setName(rs.getString(2));
		s.setStreet(rs.getString(3));
		s.setCity(rs.getString(4));
		s.setZip(rs.getInt(5));
		s.setState(rs.getString(6));
			}// while closer;
			
			
			System.out.println("The list of Student retrived from data");
			for(Student s:studentList){
				System.out.println(s);				
			}// for closer while printing studentList
			
			
			studentList.removeAll(studentList);
		} catch (SQLException e1) {
			System.out.println("Statement for creatingStatement not working");
			e1.printStackTrace();
		}
	}// actionPerformed method closer
} //class SdataRetriver closer

public class CourseRecorder implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			readCourseForm();
			writeCourseForm();
			if(clickCountC>0){
				System.out.println(courseList.get(clickCountC-1));
			}
			
		}
		
	}
public class CourseRetriver implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		if(courseList!=null){
			courseList.removeAll(courseList);
		}
		if(conn==null){
			try {
				conn=UtilityClass.getConnection();
			} catch (SQLException e1) {
				System.out.println("Connection is not reachable or connectable");
				e1.printStackTrace();
			}
		}// if closer for connection
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery("select * from course");
			//studentList=new ArrayList<Student>();
			while(rs.next()){
		Course c=new Course();
		courseList.add(c);
		c.setCourseID(rs.getInt(1));
		c.setName(rs.getString(2));
		c.setCallNo(rs.getInt(3));
		c.setInstructor(rs.getString(4));
		}// while closer;
			
			
			System.out.println("The list of Course retrived from course table");
			for(Course c:courseList){
				System.out.println(c);				
			}// for closer while printing studentList
			
			
			courseList.removeAll(courseList);
		} catch (SQLException e1) {
			System.out.println("Statement for creatingStatement not working");
			e1.printStackTrace();
		}
	}	//actionPerformed method closer
	}// class CourseRetriver closer

public class RegistrationRecorder  implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		
			readRegistrationForm();
			writeRegistrationForm();
			if(clickCountR>0){
				System.out.println(registrationList.get(clickCountR-1));
			}
			
		}// actionPerform method closer
	}// class RegistrationRecorder closer
public class RegistrationRetriver implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println("actionPerformed active on line 275");
		if(registrationList!=null){
			registrationList.removeAll(registrationList);
		}
		if(conn==null){
			try {
				conn=UtilityClass.getConnection();
			} catch (SQLException e1) {
				System.out.println("Connection is not reachable or connectable");
				e1.printStackTrace();
			}
		}// if closer for connection
		try {
			Statement stmt=conn.createStatement();
			ResultSet rs=stmt.executeQuery("select * from registration");
			//studentList=new ArrayList<Student>();
			while(rs.next()){
		Registration r=new Registration();
		registrationList.add(r);
		r.setName(rs.getString(1));
		r.setDepartment(rs.getString(2));
		r.setStudent_StudentID(rs.getInt(3));
		r.setCourse_CourseID(rs.getInt(4));
		}// while closer;
			
			
			System.out.println("The list of Course retrived from registration table");
			for(Registration r:registrationList){
				System.out.println(r);				
			}// for closer while printing studentList
			
			
			registrationList.removeAll(registrationList);
		} catch (SQLException e1) {
			System.out.println("Statement for creatingStatement not working");
			e1.printStackTrace();
		}
		
	}}

	public void readStudentForm(){
		Student student=new Student();
		
		studentList.add(student);
		
		if(!tfID.getText().isEmpty()){
			student.setStudentID(Integer.valueOf(tfID.getText()));
		//	System.out.println("Things have been written");
		}
		else {
			System.out.println("Student ID is empty");
		}
		
		
		if(!tfName.getText().isEmpty()){
			student.setName(tfName.getText());;
		}
		else {
			System.out.println("Student Name is empty");
		}
		
		
		if(!tfStreet.getText().isEmpty()){
			student.setStreet(tfStreet.getText());;
		}
		else {
			System.out.println("Student Street is empty");
		}
		
		if(!tfCity.getText().isEmpty()){
			student.setCity(tfCity.getText());;
		}
		else {
			System.out.println("Student City is empty");
		}
		
		
		if(!tfZip.getText().isEmpty()){
			student.setZip(Integer.valueOf(tfZip.getText()));
		}
		else {
			System.out.println("Student Zip is empty");
		}
			
		if(!tfState.getText().isEmpty()){
			student.setState(tfState.getText());;
		}
		else {
			System.out.println("Student State is empty");
		}
		
		
			
	}// readStudentForm method closer
	public void writeStudentForm(){
		if(clickCountS==0){
		try {
		 conn=	UtilityClass.getConnection();
		 System.out.println("connection is established with mySql");
		 clickCountS=clickCountS+1;
		} catch (SQLException e1) {
			System.out.println("connection block could not be reached");
					e1.printStackTrace();
		}
	}// if closer for intialization of connection from utility class
	try {
		PreparedStatement pstmt=conn.prepareStatement("INSERT INTO studentdb2.student(studentID,name,street, city,zip, state) VALUES (?,?,?,?,?,?)");
		System.out.println("value of clickCount= "+clickCountS);
		System.out.println(studentList.get(clickCountS-1).getStudentID());
		pstmt.setInt(1, studentList.get(clickCountS-1).getStudentID());
		pstmt.setString(2, studentList.get(clickCountS-1).getName());
		pstmt.setString(3, studentList.get(clickCountS-1).getStreet());
		pstmt.setString(4, studentList.get(clickCountS-1).getCity());
		pstmt.setInt(5, studentList.get(clickCountS-1).getZip());
		pstmt.setString(6, studentList.get(clickCountS-1).getState());
		System.out.println("data is being inserted into database studentdb2");
		pstmt.execute();
		
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}	
		
	}

	public void readCourseForm(){
		Course course =new Course();
		courseList.add(course);
		if(!CourseForm.getTfID().getText().isEmpty()){
			course.setCourseID(Integer.valueOf(CourseForm.getTfID().getText()));
		}
		
		if(!CourseForm.getTfName().getText().isEmpty()){
			course.setName(CourseForm.getTfName().getText());
		}
		
		if(!CourseForm.getTfCallNo().getText().isEmpty()){
			course.setCallNo(Integer.valueOf(CourseForm.getTfCallNo().getText()));
		}
		
		if(!CourseForm.getTfInstructor().getText().isEmpty()){
			course.setInstructor(CourseForm.getTfInstructor().getText());
		}
		
	}//readCourseForm method closer
	public void writeCourseForm(){
		
		if(clickCountC==0){
			try {
			 conn=	UtilityClass.getConnection();
			 System.out.println("connection is established with mySql for course");
			 clickCountC=clickCountC+1;
			} catch (SQLException e1) {
				System.out.println("connection block could not be reached");
						e1.printStackTrace();
			}
		}// if closer for intialization of connection from utility class
		try {
			PreparedStatement pstmt=conn.prepareStatement("INSERT INTO studentdb2.course(courseID,name, callNo, instructor) VALUES (?,?,?,?)");
			System.out.println("value of clickCountC= "+clickCountC);
			System.out.println(courseList.get(clickCountC-1).getCourseID());
			pstmt.setInt(1, courseList.get(clickCountC-1).getCourseID());
			pstmt.setString(2, courseList.get(clickCountC-1).getName());
			pstmt.setInt(3, courseList.get(clickCountC-1).getCallNo());
			pstmt.setString(4, courseList.get(clickCountC-1).getInstructor());
			
			System.out.println("data is being inserted into database studentdb2.course");
			pstmt.execute();
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
	}
	
	public void readRegistrationForm(){
		Registration registration =new Registration();
		registrationList.add(registration);
		if(!RegistrationForm.getTfName().getText().isEmpty()){
			registration.setName((RegistrationForm.getTfName().getText()));
		}
		
		if(!RegistrationForm.getTfDepartment().getText().isEmpty()){
			registration.setDepartment(RegistrationForm.getTfDepartment().getText());
		}
		
		if(!RegistrationForm.getTfStudent_StudentID().getText().isEmpty()){
			registration.setStudent_StudentID(Integer.valueOf(RegistrationForm.getTfStudent_StudentID().getText()));
		}
		
		if(!RegistrationForm.getTfCourse_CourseID().getText().isEmpty()){
			registration.setCourse_CourseID(Integer.valueOf(RegistrationForm.getTfCourse_CourseID().getText()));
		}
		
	}//readRegistrationForm method closer
	public void writeRegistrationForm(){
		if(clickCountR==0){
			try {
			 conn=	UtilityClass.getConnection();
			 System.out.println("connection is established with mySql for Registration");
			 clickCountR=clickCountR+1;
			} catch (SQLException e1) {
				System.out.println("connection block could not be reached");
						e1.printStackTrace();
			}
		}// if closer for intialization of connection from utility class
		try {
			PreparedStatement pstmt=conn.prepareStatement("INSERT INTO studentdb2.registration(name,department, Student_studentID, Course_courseID) VALUES (?,?,?,?)");
			System.out.println("value of clickCountR= "+clickCountR);
			System.out.println(registrationList.get(clickCountR-1).getName());
			pstmt.setString(1, registrationList.get(clickCountR-1).getName());
			pstmt.setString(2, registrationList.get(clickCountR-1).getDepartment());
			pstmt.setInt(3, registrationList.get(clickCountR-1).getStudent_StudentID());
			pstmt.setInt(4, registrationList.get(clickCountR-1).getCourse_CourseID());
			
			System.out.println("data is being inserted into database studentdb2.registration");
			pstmt.execute();
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
	}// writeRegistrationForm closer
}// class closer




